﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
/// <summary>
/// 耗时监测
/// </summary>
namespace Web.Filters
{
    public class MyActionFilter : IActionFilter
    {

        /// <summary>
        /// 耗时监控
        /// </summary> 
  
        Stopwatch stopwatch = new Stopwatch(); 

        public  void OnActionExecuted(ActionExecutedContext context)
        {
            //throw new NotImplementedException();
            //const long numIterations = 10000;
            stopwatch.Stop(); 
            Console.WriteLine($"{context.RouteData.Values["Controller"] +"-"+ context.RouteData.Values["Action"]}，用时{  stopwatch.Elapsed.TotalSeconds }秒" );
        }

        public  void OnActionExecuting(ActionExecutingContext context)
        {
            //重置测量
            stopwatch.Reset();
            stopwatch.Restart();
            //throw new NotImplementedException();
            stopwatch.Start(); 
        }
    }
}
